package com.brkj.main;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.brkj.course.RecodeStudyTimes;
import com.brkj.course.RecordTime;
import com.brkj.main.R;
import com.brkj.util.ConstAnts;
import com.brkj.util.MyApplication;
import com.brkj.util.SharePrefSaver;
import com.brkj.util.view.MyScrollLayout;
import com.brkj.util.view.MyScrollLayout.OnViewChangeListener;

public class GuideActivity extends Activity {

	private MyScrollLayout mScrollLayout;
	private ImageView[] imgs;
	private int count;
	private int currentItem;
	private LinearLayout pointLLayout;
	private SharedPreferences sharedPreferences;
	private SharedPreferences.Editor editor;
	private Button goLogin;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) 
			{
				sharedPreferences = GuideActivity.this
						.getSharedPreferences("firstused",
								MODE_WORLD_READABLE);
				Boolean firstused = sharedPreferences.getBoolean(
						"firstused", true);

				if ((firstused)
						|| ((GuideActivity.this.getIntent().getExtras() != null) && (GuideActivity.this
								.getIntent().getExtras().get("fromwho")
								.equals("aboutus")))) {
					System.out.println(firstused);
					setContentView(R.layout.whats_new); 
					initView();
					
					
					goLogin = (Button) findViewById(R.id.goLogin);
					goLogin.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							sharedPreferences = GuideActivity.this.getSharedPreferences("firstused",
									MODE_WORLD_READABLE);
							editor = sharedPreferences.edit();
							editor.putBoolean("firstused", false);
							editor.commit();
							
							enter();
							
						}
					});
				} else {
					enter();
				}
			}
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		
		handler.sendEmptyMessageDelayed(1, 1000);
		
		initAction();
	}



	protected void enter() {
		Intent intent;
		SharedPreferences sp = getSharedPreferences("userInfo",
				MODE_WORLD_READABLE);
	
	SharePrefSaver saver = new SharePrefSaver(GuideActivity.this, "autoLogin");
	boolean ifAutoLogin = saver.readBool("ifAutoLogin");
	
	 if ((sp.getString("userAcount", "").equals(""))
				|| (sp.getInt("userID", 0) == 0)) {
			intent = new Intent(GuideActivity.this,
					LoginActivity.class);
		} else {
			
			//调试代码
			if(MyApplication.isDebug){
				ifAutoLogin = false;
			}
			
			if(ifAutoLogin){
				intent = new Intent(GuideActivity.this,
						MainTabActivity.class);
			}else{
				intent = new Intent(GuideActivity.this,
						LoginActivity.class);
			}

		}
		GuideActivity.this.startActivity(intent);
		GuideActivity.this.finish();
	}



	private void initAction() {
		uploadCourseStudyTimes(); //学习次数
		uploadStudyTime();        //学习时长
		getMyInfo();
	}

	private void getMyInfo() {
		MyInfo myinfo = new MyInfo(this);
		myinfo.getInfoFromNet();
		
	}



	private void uploadStudyTime() {
		RecordTime studyTimes = new RecordTime(this);
		studyTimes.uploadLocalData();
		
	}


	private void uploadCourseStudyTimes() {
		RecodeStudyTimes studyTimes = new RecodeStudyTimes(this);
		studyTimes.uploadLocalData();
	}



	private void SaveVersion() {
		SharedPreferences sharedPreferences = getSharedPreferences(
				"version", MODE_WORLD_READABLE);
		Editor editor = sharedPreferences.edit();
		editor.putString("versionCode", ConstAnts.VERSION);
		editor.commit();
	}

	private String GetVersion() {
		SharedPreferences sharedPreferences = getSharedPreferences(
				"version", MODE_WORLD_READABLE);
		return sharedPreferences.getString("versionCode", "1.0");
	}

	private void initView() {
		mScrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout);
		pointLLayout = (LinearLayout) findViewById(R.id.llayout);
		count = mScrollLayout.getChildCount();
		imgs = new ImageView[count];
		for (int i = 0; i < count; i++) {
			imgs[i] = (ImageView) pointLLayout.getChildAt(i);
			imgs[i].setEnabled(true);
			imgs[i].setTag(i);
		}
		currentItem = 0;
		imgs[currentItem].setEnabled(false);
		mScrollLayout.SetOnViewChangeListener(new MyOnViewChangeListener());
	}

	class MyOnViewChangeListener implements OnViewChangeListener{

		@Override
		public void OnViewChange(int position) {
			if (position > count - 1) {
				sharedPreferences = GuideActivity.this.getSharedPreferences("firstused",
						MODE_WORLD_READABLE);
				editor = sharedPreferences.edit();
				editor.putBoolean("firstused", false);
				editor.commit();

				if(((GuideActivity.this.getIntent().getExtras() != null) && (GuideActivity.this
						.getIntent().getExtras().get("fromwho")
						.equals("aboutus")))){
					finish();
				}else{
					Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
					startActivity(intent);
					finish();
				}
				return;
			}
			if (position < 0 || position > count - 1 || currentItem == position) {
				return;
			}
			imgs[currentItem].setEnabled(true);
			imgs[position].setEnabled(false);
			currentItem = position;
			
		}
		
	}
	
}